package com.example.weather

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.RequestQueue
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.weather.weather.Forecast
import com.example.weather.weather.Weather
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {
    private lateinit var queue: RequestQueue
    private val baseURL = "http://t.weather.itboy.net/api/weather/city/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 初始化视图
        val listView = findViewById<android.widget.ListView>(R.id.listView)
        val textView_city = findViewById<android.widget.TextView>(R.id.textView_city)
        val textView_province = findViewById<android.widget.TextView>(R.id.textView_province)
        val textView_wendu = findViewById<android.widget.TextView>(R.id.textView_wendu)
        val textView_shidu = findViewById<android.widget.TextView>(R.id.textView_shidu)
        val imageView = findViewById<android.widget.ImageView>(R.id.imageView) // 初始化 ImageView

        // 获取城市代码
        val cityCode = intent.getStringExtra("cityCode")

        // 初始化网络请求队列
        queue = Volley.newRequestQueue(this)

        // 创建字符串请求
        val stringRequest = StringRequest(baseURL + cityCode, { response ->
            val gson = Gson()
            val weatherType = object : TypeToken<Weather>() {}.type
            val weather = gson.fromJson<Weather>(response, weatherType)
            textView_city.text = "${weather.cityInfo.city}"
            textView_province.text = "${weather.cityInfo.parent}" // 更新省份信息
            textView_wendu.text = "${weather.data.wendu}°C" // 添加温度单位
            textView_shidu.text = "${weather.data.shidu}" // 添加湿度单位

            val firstDay = weather.data.forecast.first()
            when (firstDay.type) {
                "晴" -> imageView.setImageResource(R.drawable.sun)
                "多云" -> imageView.setImageResource(R.drawable.mcloud)
                "阴" -> imageView.setImageResource(R.drawable.cloud)
                "阵雨" -> imageView.setImageResource(R.drawable.rain)
                else -> imageView.setImageResource(R.drawable.thunder)
            }


            val adapter = ArrayAdapter<Forecast>(this, android.R.layout.simple_list_item_1, weather.data.forecast)
            listView.adapter = adapter


            Log.d("MainActivity2", "${weather.cityInfo.city} ${weather.cityInfo.parent}")
        }, { error ->
            Log.d("MainActivity2", "Error: $error")
        })

        // 添加请求到队列
        queue.add(stringRequest)
    }
}